# 正则表达式
import re

# 语法复杂 通用性强
# match 匹配开头
# res = re.match("he", "hello word")
# print(res)

# 匹配单字符
# . 匹配任意一个字符
# res = re.match(".", "hello word")
# print(res)
# [] 匹配[]中列举的字符
# res = re.match("[he].", "hello word")
# print(res.group())
# 匹配 0-9
# res = re.match("[0123456789]", "2374532")
# res = re.match("[0-9]", "2374532")
# res = re.match("[0-45-9]", "2374532")
# print(res.group())
# res = re.match("[a-zA-Z]", "Hello Word")
# print(res.group())

# \d 匹配数字 0-9
# res = re.match(r".\d\d", "s52g34sd5")
# print(res)
# \D 匹配非数字
# res = re.match(r"\D\d", "s35s35")
# print(res)
# \s 匹配空白
# res = re.match(r"\s.", " hello word")
# print(res)
# \S 匹配非空白
# res = re.match(r"\S.", "Hello Word")
# print(res)
# \w 匹配单词字符 a-z A-Z 0-9 汉字 _
# res = re.match(r"\w", "师zhangsan")
# print(res)
# \W 匹配非单词
# res = re.match(r"\W", "+zhangsan")
# print(res)

# 匹配多字符
# * 匹配前一个字符 出现 0 次或无限次 可有可无
# res = re.match(r"\d*", "zhangsan")
# print(res)
# + 匹配前一个字符 出现 1 次或无限次 至少一次
# res = re.match(r"\d+", "34zhangsan")
# print(res)
# ? 匹配前一个字符 出现 1 次或0次
# res = re.match(r"\s?", "34zhangsan")
# print(res)
# {m} 匹配前一个字符出现m次
# res = re.match(r"\d{2}", "34zhangsan")
# print(res)
# {m,n} 匹配前一个字符出现从m次到n次 m < n
# res = re.match(r"\d{1,7}", "34wzhangsan")
# print(res)

# ^ 匹配开头和结尾 ， 表示对...取反
# res = re.match("^py", "python")
# print(res)
# 匹配 除去py 之外的
# res = re.match("[^py]", "Python")
# print(res)
# $ 匹配字符串结尾
# res = re.match(r".{7}\w$", "zhangsan")
# print(res)

# 匹配分组
# | 匹配左右任意一个表达式
# res = re.match("abc|deg", "degabcdefg")
# print(res)

# res = re.match(r"\d|\S", "degabcdefg")
# print(res)

# (ab) 匹配括号中字符作为一个分组
# res = re.match(r"\w*@(qq|163|126).com", "fengxiaoqi@qq.com")
# print(res)

# \num 匹配分组num匹配到的字符串 从外到内排序 编号从 1开始
# res = re.match(r"<(\w*)>\w*</\1>", "<html>login</html>")
# print(res)
# res = re.match(r"<(\w*)><(\w*)>.*</\2></\1>", "<html><body>login</body></html>")
# print(res)

# (?P<name>) 分组起别名
# (?P=name) 引用别名name
# res = re.match(r"<(?P<L1>\w*)><(?P<L2>\w*)>.*</(?P=L2)></(?P=L1)>", "<html><body>login</body></html>")
# print(res)

# 匹配网址
# li = ['www.baidu.com', 'www.python.org', 'http.jd.cn', 'www.py.cn', 'www.abc.cn']
# for i in li:
#     res = re.match(r"www(\.)\w*\1(com|cn|org)", i)
#     if res is not None:
#         print(res)
#     else:
#         print("网址错误：", i)

# search() 扫描整个字符串并返回第一个成功匹配的对象
# res = re.search(r"\d", "pyt2h6on")
# print(res)

# findall() 从头到尾匹配找到所有匹配成功的数据
# res = re.findall(r"\d", "pyt2h6on")
# print(res)

# sub() 替换
# res = re.sub("zhangsan", "lisi", "hello zhangsan python", 1)
# print(res)

# split() 分割
# res = re.split(" ", "hello zhangsan python")
# print(res)

# 贪婪（默认）与非贪婪
# res = re.match("em*", "emememememmem....")
# print(res)

# res = re.match("em*?", "emememememmem....")
# print(res)
# 非贪婪匹配
# res = re.match("m{1,3}?", "mmmmmmmm....")
# print(res)

res = re.match(r"\\\\\\", r"\\\name")
print(res.group())
